package com.chinamobile.iot.mqtt.spi.impl.security;

import com.chinamobile.iot.extend.auth.IMqttAuthRestClient;
import com.chinamobile.iot.mqtt.spi.security.IAuthenticator;
import com.chinamobile.iot.mqtt.spi.security.MqttClientInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Component;

@Component
@ConditionalOnExpression("${mqtt.anonymous}==false ")
public class BaasUserAuthenticator implements IAuthenticator {
    private static final int CLIENT_ID_LENGTH = 2;
    private static final String UNAVAILABLE_USER = "Bad User";
    @Autowired
    private IMqttAuthRestClient mqttAuthRestClient;

    @Override
    public MqttClientInfo loginCheck(String clientId, String username, String password) {
        MqttClientUserInfo userMqttClientInfo;
        try {
            //clientId和username必须一致
            if (!clientId.equals(username)) {
                return null;
            }
            //获取用户信息，用户名不合规时，用户信息为null
            userMqttClientInfo = clientIdValid(clientId);
            //检测用户名密码是否正确
            if (null != userMqttClientInfo) {
                String responseBody = mqttAuthRestClient.checkPassword(userMqttClientInfo.getAppToken(), userMqttClientInfo.getLoginName(), password);
                if (UNAVAILABLE_USER.equals(responseBody)) {
                    userMqttClientInfo = null;
                } else {
                    userMqttClientInfo.setMqttId(responseBody);
                }
            }
        } catch (Exception e) {
            userMqttClientInfo = null;
        }
        return userMqttClientInfo;
    }

    private MqttClientUserInfo clientIdValid(String clientId) {
        if (StringUtils.isEmpty(clientId)) {
            return null;
        }
        String[] clientIds = clientId.split("_");
        if (clientIds.length != BaasUserAuthenticator.CLIENT_ID_LENGTH) {
            return null;
        }
        String appToken = clientIds[0];
        String loginName = clientIds[1];
        return new MqttClientUserInfo(appToken, loginName);
    }

    @Override
    public String getRspTopic(MqttClientInfo clientInfo, String clientId) {
        return null;
    }

    @Override
    public void onClientDisconnected(String clientId, String mqttId) {

    }

    public class MqttClientUserInfo implements MqttClientInfo {

        private String appToken;
        private String loginName;
        private String mqttId;

        private MqttClientUserInfo(String appToken, String loginName) {
            this.appToken = appToken;
            this.loginName = loginName;
        }

        public void setMqttId(String mqttId) {
            this.mqttId = mqttId;
        }

        private String getAppToken() {
            return appToken;
        }

        private String getLoginName() {
            return loginName;
        }

        @Override
        public String getId() {
            return mqttId;
        }
    }
}


